var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var template = require("art-template");
var multer = require("multer")();
var cookieSession = require("cookie-session");
const { initConnect } = require("./routes/database.js");
initConnect();
var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var registerRouter = require("./routes/register");
var loginRouter = require("./routes/login");
var authRouter = require("./routes/auth");
var logoutRouter = require("./routes/logout");
var deleteRouter = require("./routes/delete");

var app = express();

// view engine setup
app.set("views", path.join(__dirname, "views"));
app.engine("art", require("express-art-template"));
app.set("view engine", "art");

app.use(
  cookieSession({
    name: "session",
    keys: ["key1", "key2"],
  })
);
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(multer.none());
app.use(express.static(path.join(__dirname, "public")));

app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/register", registerRouter);
app.use("/login", loginRouter);
app.use("/auth", authRouter);
app.use("/logout", logoutRouter);
app.use("/delete", deleteRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render("error");
});

module.exports = app;

var token = require("jsonwebtoken");
var fsp = require("fs/promises");

/* setImmediate(function() {
  console.log('setImmediate')
})
setTimeout(function() {
  console.log('setTimeout')
}, 0)
for (let i = 0; i < 100000; i++) {}   */
// inits();
// async function inits() {
//   // 服务器使用私钥做加密，发送给客户端
//   var privateKey = await fsp.readFile(
//     path.join(__dirname, "./rsa_private_key.pem")
//   );
//   var key = token.sign({ user: "xietian", password: "xie12345" }, privateKey, {
//     algorithm: "RS256",
//   });
//   console.log(key);
//   客户端用公钥来解密;
//   var s =
//     "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoieGlldGlhbiIsInBhc3N3b3JkIjoieGllMTIzNDUiLCJpYXQiOjE2NTA2MTgwMjV9.vZAWN7_cii5v91Y0lqwU9G8maupV7lHeH8yE22jWjgNKvxEI6IxGV_mp-hcjevKsvLNijb-uR5DyZvYj5izJQ4IPMMLfOJgDC0Ap0ZWVGQw4GGUgwc2QNefLP55MNkazAHhtzalSslQFLB3wEljRppKtuyobXnns-rpYeOPnIewLSqm1bQKenXdpCtui_MHYNp01DBElfJTcF_Mnp-hphVxcafdZl4Wxa6OSimG2dJMuKHXrcLkn-V6yhJqCY6S6QNIwxAb5QBSxGzDYZWfHdGYlOgoZ7Ih1fhBKTpIrJkA_vDPSpWHQzkE2TQUW4iA9MmQDInd2P2Ld0xL6Qrkdaw";
//   var publicKey = await fsp.readFile(
//     path.join(__dirname, "./rsa_public_key.pem")
//   );
//   var o = token.verify(s, publicKey);
//   console.log(o);
// }
